package org.example.maven.service.Impl;

import com.github.pagehelper.PageHelper;
import jakarta.transaction.Transactional;
import org.example.maven.mapper.userMapper;
import org.example.maven.pojo.user.User;
import org.example.maven.pojo.user.list.AddUser;
import org.example.maven.pojo.user.list.SearchUser;
import org.example.maven.pojo.user.list.UserListTable;
import org.example.maven.pojo.user.person.PersonDetails;
import org.example.maven.pojo.user.person.PersonUpdatePassword;
import org.example.maven.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class userServiceImpl implements UserService {
    @Autowired
    private userMapper usermapper;
    //实现用户登录
    @Override
    public User login(String username, String password) {
        return usermapper.findUser(username);
    }

    @Override
    //获取用户的权限
    public List<String> getRolePermission(Integer id) {
        return usermapper.getRolePermission(id);
    }

    @Override
    //根据用户名获取id
    public Integer getIdByUserName(String username) {
        return usermapper.getIdByUserName(username);
    }

    @Override
    //根据所有的用户
    public List<UserListTable> getAllUserList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return usermapper.getAllUserList();
    }

    @Override
    public List<UserListTable> getAllUser() {
        return usermapper.getAllUserList();
    }

    @Override
    //获取用户的单选框选项
    public List<Map<String, String>> getUserManagementSelect() {
        List<UserListTable> list=usermapper.getAllUserList();
        List<Map<String,String>> result=new ArrayList<>();
        Map<String,String> map=new HashMap<>();
        for(UserListTable u:list){
            if(!map.containsValue(u.getId())){
                Map<String,String> temp=new HashMap<>();
                temp.put("label",u.getRealName());
                temp.put("value",u.getId());
                map.put(u.getId(),u.getId());
                result.add(temp);
            }
        }
        return result;
    }

    @Override
    @Transactional
    //添加用户
    public void addUser(AddUser addUser) {
        usermapper.addUser(addUser);
        usermapper.addUserRole(addUser.getId(),addUser.getRoleId());
    }

    @Override
    //编辑用户
    public AddUser getEditUser(Integer id) {
        return usermapper.getEditUser(id);
    }

    @Override
    //根据id获取用户的角色
    public String[] getUserRole(Integer id) {
        return usermapper.getUserRole(id);
    }

    @Override
    @Transactional
    //更新用户
    public void updateUser(AddUser addUser) {
        usermapper.updateUser(addUser);
        usermapper.deleteUserRole(addUser.getId());
        usermapper.addUserRole(addUser.getId(),addUser.getRoleId());
    }

    @Override
    //删除用户
    public void deleteUser(String[] id) {
        usermapper.deleteUser(id);
    }

    @Override
    //获取特定的用户
    public List<UserListTable> getSpecieUser(SearchUser searchUser,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return usermapper.getSpecieUser(searchUser);
    }

    @Override
    //获取总用户数量
    public Integer getUserCount(SearchUser searchUser) {
        return usermapper.getUserCount(searchUser);
    }

    @Override
    //获取个人中心的信息
    public PersonDetails getPersonalDetails(String username) {
        return usermapper.getPersonalDetails(username);
    }

    @Override
    public void updatePersonDetails(PersonDetails personDetails) {
        usermapper.updatePersonDetails(personDetails);
    }

    @Override
    public void updatePersonPassword(PersonUpdatePassword personUpdatePassword) {
        usermapper.updatePersonPassword(personUpdatePassword);
    }

    @Override
    public String getPasswordById(int id) {
        return usermapper.getPasswordById(id);
    }


}
